package com.handy.socket.demo;

import java.io.IOException;
import java.nio.channels.SocketChannel;

import org.apache.log4j.Logger;

import com.handy.socket.Client;
import com.handy.socket.Reader;
import com.handy.socket.Response;

public class ClientDemo {
	public static void main(String[] args){
		ClientDemo demo = new ClientDemo();
		demo.sendXML();
	}
	
	
	public void sendXML(){
		Client client = Client.getInstance();
		//client.setHostIp("221.232.160.126");
		client.setHostIp("192.168.18.15");
		//client.setHostIp("192.168.18.10");
		
		client.setHostPort(8110);
		//client.setHostPort(9900);
		SocketChannel sc = client.getClient();
		
		Response res = new Response(sc);
		
		String str = "<?xml version=\"1.0\" encoding=\"utf-8\"?><RWMMS><id>1</id><name>维客</name><usernumber>13018011199</usernumber><imgurl>http://img2.quotes.ws.126.net/chart/timecharta/0000001.png</imgurl></RWMMS>";
		//byte[] data = new byte[str.length()];
		byte[] data = str.getBytes();
		res.send(data);
		log.info("发送完毕");
		
		
		
		byte[] msg = Reader.readRequest(sc);
		
		str = new String(msg);
		System.out.println(str);
		
		try {
			sc.close();
			sc.socket().close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private static Logger log = Logger.getLogger(ClientDemo.class);
}
